import { resolve } from 'path'
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'

import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
import vueSetupExtend from 'vite-plugin-vue-setup-extend'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'

import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'


// 参考：https://cn.vitejs.dev/config/
export default defineConfig({
  optimizeDeps: {
    include: [
      '@microsoft/fetch-event-source',
    ]
  },
  base: '/',
  resolve: {
    // 配置别名
    alias: {
      '@': resolve(__dirname, './src'),
      'vue-i18n': 'vue-i18n/dist/vue-i18n.cjs.js'
    }
  },
  plugins: [
    vue(),
    vueSetupExtend(),
    AutoImport({
      resolvers: [ElementPlusResolver()],
    }),
    Components({
      resolvers: [
        ElementPlusResolver({
          importStyle: "css", // 使用 CSS 样式
          directives: true, // 自动导入指令
        }),
      ],
    }),
    createSvgIconsPlugin({
      iconDirs: [resolve(__dirname, 'src/icons/svg')],
      symbolId: 'icon-[dir]-[name]'
    })
  ],
  server: {
    proxy: {
      '/database': {
        target: 'http://127.0.0.1:8300',
        changeOrigin: true,
        // 添加配置验证代理（新增以下配置）
        configure: (proxy) => {
          proxy.on('proxyReq', (proxyReq, req) => {
            console.log('[PROXY]', req.method, req.url, '->', proxy)
          })
        }
      },
      '/api': {
        target: 'http://localhost:8301',
        changeOrigin: true,
        configure: (proxy) => {
          proxy.on('proxyReq', (proxyReq, req) => {
            console.log('[PROXY]', req.method, req.url, '->', proxy)
          })
        }
      }

    },
    host: '0.0.0.0',
    port: 3000,
    open: false,
    headers: {
      'X-Content-Type-Options': 'nosniff',
      'X-Frame-Options': 'DENY',
      'Permissions-Policy': "geolocation=(), microphone=()",
      'Content-Security-Policy':
          "default-src 'self';" +
          "script-src 'self' 'unsafe-eval';" +
          "style-src 'self' 'unsafe-inline';" +
          "img-src 'self' data:;" +
          "font-src 'self' data:;" +
          "connect-src 'self';",
      'Referrer-Policy': 'strict-origin-when-cross-origin'
    }
  }
})
